<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务时间" prop="queryServiceDate">
        <el-date-picker clearable
          v-model="queryServiceDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="服务结束时间" prop="serviceEndDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.serviceEndDate"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择服务结束时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品型号" prop="productModel">
        <el-input
          v-model="queryParams.productModel"
          placeholder="请输入产品型号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务人员" prop="serviceStaff">
        <el-input
          v-model="queryParams.serviceStaff"
          placeholder="请输入服务人员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售员" prop="salesperson">
        <el-input
          v-model="queryParams.salesperson"
          placeholder="请输入销售员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sales:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sales:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sales:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['sales:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="售后服务记录表ID" align="center" prop="saleId" v-if="false"/>
      <el-table-column label="服务时间" align="center" prop="serviceStartDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.serviceStartDate, '{y}-{m}-{d}') }}-{{ parseTime(scope.row.serviceEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="服务结束时间" align="center" prop="serviceEndDate" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.serviceEndDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="用户名称" align="center" prop="username" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品型号" align="center" prop="productModel" />
      <el-table-column label="服务内容" align="center" prop="serviceContent" />
      <el-table-column label="产品编号" align="center" prop="productNumber" />
      <el-table-column label="服务人员" align="center" prop="serviceStaff" />
      <el-table-column label="销售员" align="center" prop="salesperson" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sales:record:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sales:record:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后服务记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务时间" prop="serviceStartDate">
<!--          <el-date-picker clearable-->
<!--            v-model="serviceDate"-->
<!--            type="datetime"-->
<!--            value-format="YYYY-MM-DD HH:mm:ss"-->
<!--            placeholder="请选择服务开始时间">-->
<!--          </el-date-picker>-->
          <el-date-picker clearable
                          v-model="serviceDate"
                          type="daterange"
                          value-format="YYYY-MM-DD"
                          range-separator="-"
                          start-placeholder="开始时间"
                          end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="服务结束时间" prop="serviceEndDate">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.serviceEndDate"-->
<!--            type="datetime"-->
<!--            value-format="YYYY-MM-DD HH:mm:ss"-->
<!--            placeholder="请选择服务结束时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品型号" prop="productModel">
          <el-input v-model="form.productModel" placeholder="请输入产品型号" />
        </el-form-item>
        <el-form-item label="服务内容" prop="serviceContent">
          <el-input v-model="form.serviceContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="产品编号" prop="productNumber">
          <el-input v-model="form.productNumber" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="服务人员" prop="serviceStaff">
          <el-input v-model="form.serviceStaff" placeholder="请输入服务人员" />
        </el-form-item>
        <el-form-item label="销售员" prop="salesperson">
          <el-input v-model="form.salesperson" placeholder="请输入销售员" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Record">
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/sales/record";
import {ref} from "vue";

const { proxy } = getCurrentInstance();

const recordList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const queryServiceDate = ref([]);
const serviceDate = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serviceStartDate: undefined,
    serviceEndDate: undefined,
    username: undefined,
    productName: undefined,
    productModel: undefined,
    productNumber: undefined,
    serviceStaff: undefined,
    salesperson: undefined,
  },
  rules: {
    saleId: [
      { required: true, message: "售后服务记录表ID不能为空", trigger: "blur" }
    ],
    serviceStartDate: [
      { required: true, message: "服务时间不能为空", trigger: "blur" }
    ],
    // serviceStartDate: [
    //   { required: true, message: "服务开始时间不能为空", trigger: "blur" }
    // ],
    // serviceEndDate: [
    //   { required: true, message: "服务结束时间不能为空", trigger: "blur" }
    // ],
    username: [
      { required: true, message: "用户名称不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "产品名称不能为空", trigger: "blur" }
    ],
    serviceContent: [
      { required: true, message: "服务内容不能为空", trigger: "blur" }
    ],
    serviceStaff: [
      { required: true, message: "服务人员不能为空", trigger: "blur" }
    ],
    salesperson: [
      { required: true, message: "销售员不能为空", trigger: "blur" }
    ],
    contact: [
      { required: true, message: "联系人不能为空", trigger: "blur" }
    ],
    contactNumber: [
      { required: true, message: "联系电话不能为空", trigger: "blur" },
      { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询售后服务记录列表 */
function getList() {
  loading.value = true;
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    saleId: null,
    serviceStartDate: null,
    serviceEndDate: null,
    username: null,
    productName: null,
    productModel: null,
    serviceContent: null,
    productNumber: null,
    serviceStaff: null,
    salesperson: null,
    contact: null,
    contactNumber: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  serviceDate.value = [];
  proxy.resetForm("recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  if(queryServiceDate.value && queryServiceDate.value.length > 0) {
    if(queryServiceDate.value[0]) {
      queryParams.value.serviceStartDate = queryServiceDate.value[0]
    }
    if(queryServiceDate.value[1]) {
      queryParams.value.serviceEndDate = queryServiceDate.value[1]
    }
  }
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryServiceDate.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.saleId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加售后服务记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _saleId = row.saleId || ids.value
  getRecord(_saleId).then(response => {
    loading.value = false;
    form.value = response.data;
    if(form.value.serviceStartDate) {
      serviceDate.value.push(form.value.serviceStartDate)
    }
    if(form.value.serviceEndDate) {
      serviceDate.value.push(form.value.serviceEndDate)
    }
    open.value = true;
    title.value = "修改售后服务记录";
  });
}

/** 提交按钮 */
function submitForm() {
  if(serviceDate.value && serviceDate.value.length > 0) {
    if(serviceDate.value[0]) {
      form.value.serviceStartDate = serviceDate.value[0]
    }
    if(serviceDate.value[1]) {
      form.value.serviceEndDate = serviceDate.value[1]
    }
  }
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.saleId != null) {
        updateRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _saleIds = row.saleId || ids.value;
  proxy.$modal.confirm('是否确认删除售后服务记录编号为"' + _saleIds + '"的数据项？').then(function() {
    loading.value = true;
    return delRecord(_saleIds);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sales/record/export', {
    ...queryParams.value
  }, `record_${new Date().getTime()}.xlsx`)
}

getList();
</script>
